import Vue from 'vue'

export const fetchToken  = ({ commit },{username,password}) => {
		return Vue.http.post('api/token/getTokenByAccount.action',{},{
			params:{
				account:username,
				password:password
			},
			showError:1,
		}).then(function(res){
			let token=res.body.result.accessToken;
			alert("token:"+token);
			commit('SET_TOKEN',token);
			window.localStorage.setItem('token',token);
			Vue.http.headers.common['Authorization'] = token;
			commit('SET_USERNAME',username);
			window.localStorage.setItem('username',username);
			return Promise.resolve(token);
		})
};
export const userLogout  = ({ commit},{username,token}) => {
	return Vue.http.post('login/loginout',{},{
		params:{
			username:username,
			token:token
		}
	}).then(function(){
		commit('UNSET_USER');
		window.localStorage.removeItem('token');
		window.localStorage.removeItem('username');
		delete Vue.http.headers.common['Authorization'];
	},function(){
		//失败了直接前端注销掉
		commit('UNSET_USER');
		window.localStorage.removeItem('token');
		window.localStorage.removeItem('username');
		delete Vue.http.headers.common['Authorization'];
	})
};
//时间关系，后面的就不计入store了

/**
 * 获取审查信息
 * @type {function()}
 */
export const fetchOrder=(({commit})=>{
	return Vue.http.post('api/shopinfo/list.action',{},{
		params:{},
		showError:1,
	}).then(function(res){
		let shops=res.body.result;
		commit('FETCH_ORDER_SUCCESS',shops);
	})
})
/**
 * 获取用户信息
 * @type {Function}
 */
export const fetchOrderUserInfo=(({commit},{userId})=>{
	return Vue.http.post('instalment/queryAllUserInfo',{},{
		params:{
			userId:userId
		},
		showError:1,
	}).then(function(res){
		let info=res.data.data[0];
		return Promise.resolve(info);
	})
})
/**
 * 获取订单分期信息（几期，分期类型）
 * @type {Function}
 */
export const fetchOrderInstallment=(({commit},{id})=>{
	return Vue.http.post('instalment/queryInstalmentType',{},{
		params:{
			id:id
		},
		showError:1,
	}).then(function(res){
		let info=res.data.data[0];
		return Promise.resolve(info);
	})
})
/**
 * 获取订单还款计划
 * @type {Function}
 */
export const fetchOrderRepaymentPlan=(({commit},{orderId})=>{
	return Vue.http.post('instalment/queryRepaymentPlan',{},{
		params:{
			orderId:orderId
		},
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
/**
 * 获取用户的审核信息
 * @type {Function}
 */
export const fetchOrderReviewInfo=(({commit},params)=>{
	return Vue.http.post('instalment/queryUserInvestigationInfo',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
/**
 * 更新用户审核信息
 * @type {Function}
 */
export const updateReview=(({commit},params)=>{
	return Vue.http.post('instalment/updateUserInvestigationInfo',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
/**
 * 更新用户审核状态
 * @type {Function}
 */
export const updateReviewStatus=(({commit},params)=>{
	return Vue.http.post('instalment/investigationOrder',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
/**
 * 更新用户审批状态
 * @type {Function}
 */
export const updateApprovalStatus=(({commit},params)=>{
    return Vue.http.post('instalment/approvalOrder',{},{
        params:params,
        showError:1,
    }).then(function(res){
        let info=res.data.data;
        return Promise.resolve(info);
    })
})
/**
 * 更新用户审批结论
 * @type {Function}
 */
export const updateApproval=(({commit},params)=>{
    return Vue.http.post('instalment/approvalConclusion',{},{
        params:params,
        showError:1,
    }).then(function(res){
        let info=res.data.data;
        return Promise.resolve(info);
    })
})
export const fetchBlacklist=(({commit},params)=>{
	return Vue.http.post('instalment/queryBlackList',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
export const addBlacklist=(({commit},params)=>{
	return Vue.http.post('instalment/addBlackList',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
export const delBlacklist=(({commit},params)=>{
	return Vue.http.post('instalment/deleteBlackList',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
export const fetchAllUserInfo=(({commit})=>{
	return Vue.http.post('instalment/queryAllUserInfo',{},{
		params:{},
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})

export const fetchLoans=(({commit},params)=>{
	return Vue.http.post('instalment/queryPtln',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
export const uploadLoans=(({commit},params)=>{
	return Vue.http.post('instalment/uploadPtln',{},{
		params:params,
		showError:1,
	}).then(function(res){
		let info=res.data.data;
		return Promise.resolve(info);
	})
})
